package rok3.zesp.projekt.screens;

import rok3.zesp.projekt.OurGame;
import rok3.zesp.projekt.Settings;
import rok3.zesp.projekt.models.PhysicCenter;
import rok3.zesp.projekt.models.objects.Human;
import rok3.zesp.projekt.p.BottomPanel;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.input.GestureDetector.GestureAdapter;
import com.badlogic.gdx.maps.objects.PolygonMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.List;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Array;

public class FlashScreen extends AbstractScreen {
	private TextButton button;
	Stage stage;
	private TiledMap map;
	private OrthogonalTiledMapRenderer mapRenderer;
	private OrthographicCamera mapCam;

	public FlashScreen(OurGame ourGame) {

	}
	
	float acc=0;
	float update_frequency=0.1f;
	float tmp=0;
	Array<Integer > sd = new Array<Integer>();
	private TextureRegionDrawable img;
	
	@Override
	public void render(float delta) {
		super.render(delta);
		stage.act();
		
//		if (Gdx.input.isKeyPressed(Keys.LEFT )) {
//			mapCam.position.set(mapCam.position.x+1,mapCam.position.y,mapCam.position.z );
//			mapCam.update();
//			mapRenderer.setView(mapCam);
//		}

		mapRenderer.render();

		stage.draw();
		
		if ( Gdx.input.isKeyPressed(Keys.LEFT)) {
			if ( acc < update_frequency ) {
				acc += delta;
				sd.add(1);
				
			} else {
				acc = acc - update_frequency;
				System.out.println(sd.size + " times");
				sd.clear();
				
				stage.getSpriteBatch().begin();
				img.draw(stage.getSpriteBatch(), x,y,50, 65);
				stage.getSpriteBatch().end();
			}
		} else {
			stage.getSpriteBatch().begin();
			img.draw(stage.getSpriteBatch(), x,y,50, 65);
			stage.getSpriteBatch().end();
		}
		
	}
	
	float x=100f,y=50f;

	@Override
	public void show() {
		super.show();
		

		NinePatch patch = new NinePatch(new Texture(
				Gdx.files.internal("test/test.png")), 12, 12, 12, 12);

		// Create a new TextButtonStyle
		TextButtonStyle style = new TextButtonStyle();
		style.up = new NinePatchDrawable(patch);
		style.font = new BitmapFont(Gdx.files.internal("test/font.fnt"));
		final NinePatch patch2 = new NinePatch(new Texture(
				Gdx.files.internal("test/over.png")), 12, 12, 12, 12);
		style.over = new NinePatchDrawable(patch) {
			@Override
			public void draw(SpriteBatch batch, float x, float y, float width,
					float height) {
				patch2.draw(batch, x + 100, y + 50, width, height);
				super.draw(batch, x, y, width, height);
			}
		};

		button = new TextButton("hello woraaaaaaaaaaaaaald", style);
		button.addListener(new ClickListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				// Gdx.graphics.setDisplayMode(Gdx.graphics.getDesktopDisplayMode().width,
				// Gdx.graphics.getDesktopDisplayMode().height, true);
				return super.touchDown(event, x, y, pointer, button);
			}

			@Override
			public boolean isOver() {
				System.out.println("is over");
				return super.isOver();
			}
		});

		Skin skin = new Skin(Gdx.files.internal("skin/uiskin.json"));
		String[] s = { "asdasd", "12121", "333" };
		List list = new List(s, skin);

		Label l = new Label(
				"a\nsdddd\ndd\ndddddddd\nddd\nddd\ndd\nddd\nddd\nddd\ndd\ndddd",
				skin);
		ScrollPane sp = new ScrollPane(l, skin);

		sp.setPosition(200, 300);

		BottomPanel ac = new BottomPanel(skin);

		stage = new Stage();
		// stage.addActor(button);
		stage.addActor(ac);
		Gdx.input.setInputProcessor(stage);

		map = new TmxMapLoader().load("map/map2.tmx");
		mapCam = new OrthographicCamera(Settings.window_width*Settings.scale_ratioX,Settings.window_height*Settings.scale_ratioY);
		mapCam.position.set(400,300,0);
		mapCam.update();
		
		
		//System.out.println(mapCam.viewportHeight);

		float unitScale = 1f;
		mapRenderer = new OrthogonalTiledMapRenderer(map, unitScale);
		mapRenderer.setView(mapCam);
		
		Array<PolygonMapObject> ob = map.getLayers().get(2).getObjects().getByType(PolygonMapObject.class);
		System.out.println ( map.getLayers().get(2).getName());
		for (PolygonMapObject o : ob ) {
			System.out.println("*****");
			float[] k = o.getPolygon().getTransformedVertices();
			for ( float m : k )  {
				System.out.print(m+" ");
			}
			System.out.println();
		}
		
		Human human = new Human();
		PhysicCenter pc = new PhysicCenter();
		pc.registerCollisbaleObject(human);
		
		img = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("rec/di.png"))));
		
	}
}
